package edu.st.selenium.pages;

import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.ui.Select;

public class NewMoviePage extends InternalPage {
	
	public NewMoviePage(PageManager pages) {
		super(pages);
	}
	
	public NewMoviePage ensurePageLoaded() {
	    super.ensurePageLoaded();
	    wait.until(presenceOfElementLocated(By.cssSelector("img[title='Save']")));
	    return this;
	  }

	@FindBy (css = "img[title='Save']")
	private WebElement saveLink;
	
	@FindBy (name = "name")
	private WebElement titleField;

	@FindBy (name = "imdbid")
	private WebElement imdbField;
	
	@FindBy (name = "year")
	private WebElement yearField;
	
	@FindBy (name = "notes")
	private WebElement notesField;
	
	@FindBy (css = ".error[for=name]")
	private WebElement errorTitle;
	
	@FindBy (css = ".error[for=year]")
	private WebElement errorYear;

	public NewMoviePage setTitleField(String text) {
		titleField.sendKeys(text);
		return this;
	}

	public NewMoviePage setImdbField(String text) {
		imdbField.sendKeys(text);
		return this;
	}


	public NewMoviePage setYearField(String text) {
		yearField.sendKeys(text);
		return this;
	}


	public NewMoviePage setNotesField(String text) {
		notesField.sendKeys(text);
		return this;
	}

	public MoviePage clickSaveButton() {
		saveLink.click();
		return pages.moviePage;		
	}
	
	public NewMoviePage clickSaveButtonError() {
		saveLink.click();
		return pages.newMoviePage;		
	}
	
	public boolean getErrorTitle(){
		
		  try {
			    return errorTitle.isDisplayed();
			  } catch (NoSuchElementException e) {
			    return false;
			  }
			}
	
	public boolean getErrorYear(){
		  try {
			    return errorYear.isDisplayed();
			  } catch (NoSuchElementException e) {
			    return false;
			  }
			}
}
